from pylab import *
from rapameters import *
from oscillator_hash import *
from copy import *
from time import *
from raptools import *
from wave import Wave
from random import *;

#run rap6.py 
#               [tau, rp,   rm, qp, qm, m,  n,  k]
#rarray = [9.2368511295552782, 1.9767713195111938, 1.6664059123907351, 0.53661906803386461, 0.99645082014860609, 2.85751441601652, 6.1476674504460336, 2.2114851186039757]
#rarray= [3.4848824502235609, 0.8468732169965949, 1.4878051027445729, 0.20999999999999999, 0.05533504604846054, 2.9188881965559172, 2.0, 2.4264584017432957]
#rarray = [8.4488948540555491, 2.0273043988333539, 1.6098517690042096, 0.66204133845355029, 0.9276339434360743, 3.0, 3.0555585127435707, 0.087618911766850993]
#run7
#rarray = [8.4488948540555491, 2.0273043988333539, 1.6098517690042096, 0.66204133845355029, 0.9276339434360743, 3.0, 3.0555585127435707, 0.087618911766850993]
#run8
#rarray = [3.6332483954787556, 2.3989339645253507, 1.1651438517360364, 0.2778903963529038, 0.054684254029319673, 3.0, 1.7134317358685796, 1.0]
rarray = [2.9554175233221267, 5.7511329724419049,7.0048253697971283,4.3387252752962,0.87617535872549446,1.796038551629723,10.0,2.2676241471854861]

#amp:  0.772295081967
#frq:  0.180381628861
#time up:  4.28360655738
#time down:  2.00491803279
#rarray = [4.5838182561230747, 1.0, 0.95737318672125316, 1.4343376593960682, 0.72577491791276383, 3.5266787935710231, 2.9766992207883853, 0.44451464948452513]

#DT = 0.01, RND = 2
#rarray = [0.10032028214582867, 1.8012519047754036, 5.6464582648382491, 8.6074190981368073, 1.7644265038651772, 6.2102616431613544, 4.3480101508589506, 1.9440112885064706]
rarray = [0.10032028214582867, 2.1100394494218313, 6.6137851793155829, 10.65505756886763, 1.2521308852844522, 7.3682608522948732, 5.1151251764037546, 3.7635409817481222]

TIME                = 10;
DT                  = 0.01;
AMP_MAX             =  200;

start = Oscillator(rarray,TIME,DT,2, AMP_MAX)
spt = start.get_times();
spv = start.get_vals();
spm = start.get_mrna();


print "----------------------------------------------------------"
print 'OSCILLATOR : CURRENT BEST', id(start), ' (BLUE)'
print "----------------------------------------------------------"
print 'amp: ',start.get_amp();
print 'frq: ', start.get_frq();
print 'per: ', start.get_per();
print 'time up: ',start.get_tim_up();
print 'time down: ',start.get_tim_down();
print 'parameters', start.get_rapams();
print ""
print start

print ""
print ""
print ""

#########################
#MERJENJE CASA ZA PREKLOP
#DRSECE POVPRECJE
#########################
VALS = start.get_vals();
AMP = start.get_amp();
HMIN = AMP*0.8;
LMAX = AMP*0.2;
RISE = 0;
FALL = 0;
RAVG = start.get_tim_up();
FAVG = start.get_tim_down();

BOUNDS = [0 for v in VALS]
COUNT = 1
RAVGCOUNT = 1.0;
FAVGCOUNT = 1.0;

rise = True
for pval in VALS:
  if rise:
    if pval>LMAX and pval <HMIN:
        RISE = RISE + 1
    if pval > HMIN:
      RAVGCOUNT = RAVGCOUNT+1.0
      #RAVG = (RAVG + RISE*DT)/2.0;
      RAVG = ((RAVGCOUNT-1)/RAVGCOUNT)*RAVG + (1.0/RAVGCOUNT)*RISE*DT;
      RISE = 0;
      rise = False
      BOUNDS[COUNT-1] = HMIN
  else:
    if pval>LMAX and pval <HMIN:
      FALL = FALL + 1
    if pval < LMAX:
      FAVGCOUNT = FAVGCOUNT+1
      #FAVG = (FAVG + FALL*DT)/2.0;
      FAVG = ((FAVGCOUNT-1)/FAVGCOUNT)*FAVG + (1.0/FAVGCOUNT)*FALL*DT;
      FALL = 0
      rise = True
      BOUNDS[COUNT-1] = LMAX
  COUNT = COUNT + 1
  

print "Rise average:", RAVG  
print "Fall average:", FAVG  
	

#print start.get_vals()



plot(spt,spv,'k-')
plot(spt,spm,'r-', linestyle='dashed')
plot(spt,BOUNDS,'bx')
#xticks(range(0,TIME+1,10))
#yticks(range(0,int(start.get_amp())+1,1))
xlabel('Time (h)')
ylabel('Protein (full line) / mRNA (dashed line) concentration')
show()
#savefig(sys.argv[1], format='png')
